package com.auto.survey.activity.monitoring.ctrlmeasure

import android.content.Intent
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.auto.survey.R
import com.auto.survey.activity.monitoring.DoCollectedActivity
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_MEASURE_MODLE
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_OBJECT_ID
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_PROJET_ID
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_STATION_ID
import com.auto.survey.activity.monitoring.IntentKey.KEY_INTENT_TASK_ID
import com.auto.survey.activity.monitoring.IntentKey.KEY_MEASURE_MODLE1
import com.auto.survey.activity.monitoring.IntentKey.KEY_MEASURE_MODLE2
import com.auto.survey.activity.monitoring.adapter.ClosedLoopObserverAdapter
import com.auto.survey.activity.monitoring.dao.MonitoringRepository
import com.auto.survey.activity.monitoring.entity.relations.MonitoringPointWithCtrlObserv
import com.auto.survey.base.BaseActivity
import com.auto.survey.widget.dialog.radiodialog.RadioDialogHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class CtrlMeasureListActivity : BaseActivity() {
    private var projectId:Int = -1
    private var objectId:Int = -1
    private var stationId:Int = -1
    private var taskId:Int = -1
    private val database = MonitoringRepository()

    private val adapter = ClosedLoopObserverAdapter()

    override fun initView() {
        projectId = intent.getIntExtra(KEY_INTENT_PROJET_ID, -1)
        objectId = intent.getIntExtra(KEY_INTENT_OBJECT_ID, -1)
        stationId = intent.getIntExtra(KEY_INTENT_STATION_ID, -1)
        taskId = intent.getIntExtra(KEY_INTENT_TASK_ID, -1)

        mTopBar?.visibility = View.VISIBLE
        mTopBar?.setBackgroundResource(R.color.action_bar)

        mComment_title?.visibility = View.VISIBLE
        mComment_title?.setBackgroundResource(R.color.action_bar)
        mComment_title?.setTitle("数据列表")

        val recycler = findViewById<RecyclerView>(R.id.rv_ctrlmeasure_list)
        recycler.layoutManager = LinearLayoutManager(this@CtrlMeasureListActivity)
        recycler.adapter = adapter

        findViewById<TextView>(R.id.tv_ctrl_settings).setOnClickListener {
            val intent = Intent()
            intent.putExtra(KEY_INTENT_PROJET_ID, projectId)
            intent.putExtra(KEY_INTENT_OBJECT_ID, objectId)
            intent.putExtra(KEY_INTENT_STATION_ID, stationId)
            intent.putExtra(KEY_INTENT_TASK_ID, taskId)
            intent.setClass(this@CtrlMeasureListActivity, CtrlSettingsActivity::class.java)
            startActivity(intent)
        }

        findViewById<TextView>(R.id.tv_ctrl_start_measure).setOnClickListener {

            RadioDialogHelper.Builder(this)
                .setTitle("请选择")
                .setItems(listOf(
                    RadioDialogHelper.RadioBean(0x4001, KEY_MEASURE_MODLE1),
                    RadioDialogHelper.RadioBean(0x4002, KEY_MEASURE_MODLE2),
                    ))
                .setSelectedIndex(0)
                .setOnItemSelectedListener { position, radioBean ->
                    val intent = Intent()
                    intent.putExtra(KEY_INTENT_PROJET_ID, projectId)
                    intent.putExtra(KEY_INTENT_OBJECT_ID, objectId)
                    intent.putExtra(KEY_INTENT_STATION_ID, stationId)
                    intent.putExtra(KEY_INTENT_TASK_ID, taskId)
                    if (position == 0) {
                        intent.putExtra(KEY_INTENT_MEASURE_MODLE, KEY_MEASURE_MODLE1)
                    } else {
                        intent.putExtra(KEY_INTENT_MEASURE_MODLE, KEY_MEASURE_MODLE2)
                    }

                    intent.setClass(this@CtrlMeasureListActivity, CtrlStartMeasureActivity::class.java)
                    startActivity(intent)
                }.build().show()

        }

    }

    override fun onResume() {
        super.onResume()
        lifecycleScope.launch {
            val items = mutableListOf<MonitoringPointWithCtrlObserv>()
            val data = database.getTaskPointList(taskId)
            data?.points?.forEach {
                val bean = database.getMonitoringPointCtrlObservationByPid(it.pointId)
                bean?.let {
                    items.add(it)
                }
            }

            withContext(Dispatchers.Main) {
                adapter.setItems(items)
            }
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_ctrlmeasure_list
    }

}